package com.byxy.mysystem.controller.commodity;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.byxy.mysystem.model.commodity.Commodity;
import com.byxy.mysystem.service.commodity.ICommodityService;
import com.byxy.mysystem.util.JsonResult;
import com.byxy.mysystem.util.ResultTool;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author omd
 * @since 2022-09-02
 */
@RestController
@RequestMapping("/commodity")
public class CommodityController {

    @Autowired
    private ICommodityService commodityService;

    @PostMapping("/page")
    public JsonResult<IPage<Commodity>> page(@RequestBody Map<String,Object> params){
         long current = params.get("current") != null?Integer.valueOf(params.get("current").toString()):1;
         long size = params.get("size") != null?Integer.valueOf(params.get("size").toString()):10;
         IPage page = new Page(current,size);
         //
         QueryWrapper<Commodity> cWrapper = new QueryWrapper<>();
         if(params.get("id") != null){
             cWrapper.eq("id",params.get("id"));
         }
         if(params.get("keyWord") != null && StringUtils.isNotBlank(params.get("keyWord").toString())){
             cWrapper.like("name",params.get("keyWord"));
         }
         IPage<Commodity> result = commodityService.page(page,cWrapper);
         return ResultTool.success(result);
    }
    @PostMapping("/view")
    public JsonResult view(@RequestBody Commodity commodity){
        Commodity commodity1 = commodityService.getById(commodity.getId());
        return ResultTool.success(commodity1);
    }
    @PostMapping("/save")
    public JsonResult save(@RequestBody Commodity commodity){
        boolean flag = commodityService.save(commodity);
        return ResultTool.success(flag);
    }

    @PostMapping("/update")
    public JsonResult update(@RequestBody Commodity commodity){
        boolean flag = commodityService.updateById(commodity);
        return ResultTool.success(flag);
    }
    @PostMapping("/delete")
    public JsonResult delete(@RequestBody Commodity commodity){
        boolean flag = commodityService.removeById(commodity.getId());
        return ResultTool.success(flag);
    }

}
